/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.logistics.service.overseaswarehouse;

import com.jeesite.common.collect.SetUtils;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.ar.entity.Tar;
import com.jeesite.modules.ar.entity.TarDetail;
import com.jeesite.modules.ar.service.TarService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.logistics.dao.overseaswarehouse.OverseasWarehouseDao;
import com.jeesite.modules.logistics.dao.overseaswarehouse.OverseasWarehouseDetailDao;
import com.jeesite.modules.logistics.entity.overseaswarehouse.OverseasWarehouse;
import com.jeesite.modules.logistics.entity.overseaswarehouse.OverseasWarehouseDetail;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.payment.entity.TapDetail;
import com.jeesite.modules.payment.service.TapService;
import com.jeesite.modules.prepay.entity.Tprepayment;
import com.jeesite.modules.prepay.entity.TprepaymentActual;
import com.jeesite.modules.prepay.entity.TprepaymentEstimated;
import com.jeesite.modules.prepay.service.TprepaymentService;
import com.jeesite.modules.prerecieve.entity.TadvancesReceived;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedActual;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedEstimated;
import com.jeesite.modules.prerecieve.service.TadvancesReceivedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * 海外仓Service
 *
 * @author LV
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly = true)
public class OverseasWarehouseService extends CrudService < OverseasWarehouseDao, OverseasWarehouse > {

    @Autowired
    private OverseasWarehouseDetailDao overseasWarehouseDetailDao;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private TadvancesReceivedService tadvancesReceivedService;
    @Autowired
    private TapService tapService;
    @Autowired
    private TarService tarService;
    @Autowired
    private TprepaymentService tprepaymentService;


    /**
     * 获取单条数据
     *
     * @param overseasWarehouse
     * @return
     */
    @Override
    public OverseasWarehouse get(OverseasWarehouse overseasWarehouse) {
        OverseasWarehouse entity = super.get(overseasWarehouse);
        if (entity != null) {
            OverseasWarehouseDetail overseasWarehouseDetail = new OverseasWarehouseDetail(entity);
            overseasWarehouseDetail.setStatus(OverseasWarehouseDetail.STATUS_NORMAL);
            entity.setOverseasWarehouseDetailList(overseasWarehouseDetailDao.findList(overseasWarehouseDetail));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param overseasWarehouse      查询条件
     * @param overseasWarehouse.page 分页对象
     * @return
     */
    @Override
    public Page < OverseasWarehouse > findPage(OverseasWarehouse overseasWarehouse) {
        return super.findPage(overseasWarehouse);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param overseasWarehouse
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(OverseasWarehouse overseasWarehouse) {
        if (overseasWarehouse.getIsNewRecord()) {
            overseasWarehouse.setOverseasWarehouseCode(codeRuleService.getCode("overseaswarehouse"));
        }
        super.save(overseasWarehouse);
        if (overseasWarehouse.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("overseaswarehouse");
        }
        // 保存上传附件
        FileUploadUtils.saveFileUpload(overseasWarehouse.getId(), "overseasWarehouse_file");
        // 保存 OverseasWarehouse子表
        int index = 0;
        for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouse.getOverseasWarehouseDetailList()) {
            if (!OverseasWarehouseDetail.STATUS_DELETE.equals(overseasWarehouseDetail.getStatus())) {
                overseasWarehouseDetail.setOverseasWarehouseId(overseasWarehouse);
                if (overseasWarehouseDetail.getIsNewRecord()) {
                    overseasWarehouseDetailDao.insert(overseasWarehouseDetail);
                } else {
                    overseasWarehouseDetailDao.update(overseasWarehouseDetail);
                }
            } else {
                overseasWarehouseDetailDao.delete(overseasWarehouseDetail);
            }
            // 保存上传附件
            FileUploadUtils.saveFileUpload(overseasWarehouseDetail.getId(),
                    "overseasWarehouseDetailList[" + index + "].overseasWarehouseDetail_file");
            index++;
        }

        List < OverseasWarehouseDetail > overseasWarehouseDetailList = overseasWarehouse.getOverseasWarehouseDetailList();
        // 服务订单 回写
        ServiceOrder serviceOrder = serviceOrderService.get(overseasWarehouse.getServiceOrderId());
        List < ServiceOrderDetail > serviceOrderDetailList = serviceOrder.getServiceOrderDetailList();
        for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
            for (ServiceOrderDetail serviceOrderDetail : serviceOrderDetailList) {
                if (overseasWarehouseDetail.getRowNumber().equals(serviceOrderDetail.getRowNumber())) {
                    serviceOrderDetail.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                    serviceOrderDetail.setLogisticsStatus(overseasWarehouseDetail.getWarehousestatus());
                    serviceOrderDetail.setOverseasWarehouseId(overseasWarehouse.getOverseasWarehouseId());
                    serviceOrderDetail.setEngineNumber(overseasWarehouseDetail.getEngineNumber());
                    serviceOrderDetail.setColor(overseasWarehouseDetail.getColor());
                    serviceOrderDetail.setProductionDate(overseasWarehouseDetail.getProductionDate());
                    serviceOrderDetail.setFactory(overseasWarehouseDetail.getFactory());
                    serviceOrderDetail.setTireSize(overseasWarehouseDetail.getTireSize());
                    serviceOrderDetail.setSeatNumber(overseasWarehouseDetail.getSeatNumber());
                    serviceOrderDetail.setMilage(overseasWarehouseDetail.getMilage());
                }
            }
        }
        serviceOrderService.save(serviceOrder);

        // 车架号去重校验
        /*Set < String > set = SetUtils.newHashSet();
        Integer detailFramerCount = 0;
        for (ServiceOrderDetail serviceOrderDetail : serviceOrderService.findListDetail(new ServiceOrderDetail())) {
            if (StringUtils.isNotEmpty(serviceOrderDetail.getFrameNumber())) {
                set.add(serviceOrderDetail.getFrameNumber());
                detailFramerCount++;
            }
        }
        if (detailFramerCount != set.size()) {
            throw new RuntimeException("存在相同的车架号！");
        }*/

        // 预收费用 回写
        if (StringUtils.isNotEmpty(serviceOrder.getYsfyId())) {
            TadvancesReceived tadvancesReceived = tadvancesReceivedService.get(serviceOrder.getYsfyId());
            // 子表-预计
            List < TadvancesReceivedEstimated > tadvancesReceivedEstimatedList = tadvancesReceived.getTadvancesReceivedEstimatedList();
            // 子表-实际
            List < TadvancesReceivedActual > tadvancesReceivedActualList = tadvancesReceived.getTadvancesReceivedActualList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceivedEstimatedList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tadvancesReceivedEstimated.getRowNumber())) {
                        if (StringUtils.isEmpty(tadvancesReceivedEstimated.getFrameNumber())) {
                            tadvancesReceivedEstimated.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
                for (TadvancesReceivedActual tadvancesReceivedActual : tadvancesReceivedActualList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tadvancesReceivedActual.getRowNumber())) {
                        if (StringUtils.isEmpty(tadvancesReceivedActual.getFrameNumber())) {
                            tadvancesReceivedActual.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tadvancesReceivedService.save(tadvancesReceived);
        }
        // 保证金预收 回写
        if (StringUtils.isNotEmpty(serviceOrder.getBzjYusId())) {
            TadvancesReceived tadvancesReceived = tadvancesReceivedService.get(serviceOrder.getBzjYusId());
            // 子表-预计
            List < TadvancesReceivedEstimated > tadvancesReceivedEstimatedList = tadvancesReceived.getTadvancesReceivedEstimatedList();
            // 子表-实际
            List < TadvancesReceivedActual > tadvancesReceivedActualList = tadvancesReceived.getTadvancesReceivedActualList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceivedEstimatedList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tadvancesReceivedEstimated.getRowNumber())) {
                        if (StringUtils.isEmpty(tadvancesReceivedEstimated.getFrameNumber())) {
                            tadvancesReceivedEstimated.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
                for (TadvancesReceivedActual tadvancesReceivedActual : tadvancesReceivedActualList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tadvancesReceivedActual.getRowNumber())) {
                        if (StringUtils.isEmpty(tadvancesReceivedActual.getFrameNumber())) {
                            tadvancesReceivedActual.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tadvancesReceivedService.save(tadvancesReceived);
        }
        // 代理费预收 回写
        if (StringUtils.isNotEmpty(serviceOrder.getDlfYusId())) {
            TadvancesReceived tadvancesReceived = tadvancesReceivedService.get(serviceOrder.getDlfYusId());
            // 子表-预计
            List < TadvancesReceivedEstimated > tadvancesReceivedEstimatedList = tadvancesReceived.getTadvancesReceivedEstimatedList();
            // 子表-实际
            List < TadvancesReceivedActual > tadvancesReceivedActualList = tadvancesReceived.getTadvancesReceivedActualList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceivedEstimatedList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tadvancesReceivedEstimated.getRowNumber())) {
                        if (StringUtils.isEmpty(tadvancesReceivedEstimated.getFrameNumber())) {
                            tadvancesReceivedEstimated.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
                for (TadvancesReceivedActual tadvancesReceivedActual : tadvancesReceivedActualList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tadvancesReceivedActual.getRowNumber())) {
                        if (StringUtils.isEmpty(tadvancesReceivedActual.getFrameNumber())) {
                            tadvancesReceivedActual.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tadvancesReceivedService.save(tadvancesReceived);
        }
        // 保证金预付 回写
        if (StringUtils.isNotEmpty(serviceOrder.getBzjYufId())) {
            Tprepayment tprepayment = tprepaymentService.get(serviceOrder.getBzjYufId());
            // 子表-预计
            List < TprepaymentEstimated > tprepaymentEstimatedList = tprepayment.getTprepaymentEstimatedList();
            // 子表-实际
            List < TprepaymentActual > tprepaymentActualList = tprepayment.getTprepaymentActualList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TprepaymentEstimated tprepaymentEstimated : tprepaymentEstimatedList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tprepaymentEstimated.getRowNumber())) {
                        if (StringUtils.isEmpty(tprepaymentEstimated.getFrameNumber())) {
                            tprepaymentEstimated.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
                for (TprepaymentActual tprepaymentActual : tprepaymentActualList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tprepaymentActual.getRowNumber())) {
                        if (StringUtils.isEmpty(tprepaymentActual.getFrameNumber())) {
                            tprepaymentActual.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tprepaymentService.save(tprepayment);
        }
        // 代理费预付 回写
        if (StringUtils.isNotEmpty(serviceOrder.getDlfYufId())) {
            Tprepayment tprepayment = tprepaymentService.get(serviceOrder.getDlfYufId());
            // 子表-预计
            List < TprepaymentEstimated > tprepaymentEstimatedList = tprepayment.getTprepaymentEstimatedList();
            // 子表-实际
            List < TprepaymentActual > tprepaymentActualList = tprepayment.getTprepaymentActualList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TprepaymentEstimated tprepaymentEstimated : tprepaymentEstimatedList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tprepaymentEstimated.getRowNumber())) {
                        if (StringUtils.isEmpty(tprepaymentEstimated.getFrameNumber())) {
                            tprepaymentEstimated.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
                for (TprepaymentActual tprepaymentActual : tprepaymentActualList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tprepaymentActual.getRowNumber())) {
                        if (StringUtils.isEmpty(tprepaymentActual.getFrameNumber())) {
                            tprepaymentActual.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tprepaymentService.save(tprepayment);
        }
        // 代理费应收 回写
        if (StringUtils.isNotEmpty(serviceOrder.getDlfYisId())) {
            Tar tar = tarService.get(serviceOrder.getDlfYisId());
            List < TarDetail > tarDetailList = tar.getTarDetailList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TarDetail tarDetail : tarDetailList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tarDetail.getRowNumber())) {
                        if (StringUtils.isEmpty(tarDetail.getFrameNumber())) {
                            tarDetail.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tarService.save(tar);
        }
        // 代理费应付 回写
        if (StringUtils.isNotEmpty(serviceOrder.getDlfYifId())) {
            Tap tap = tapService.get(serviceOrder.getDlfYifId());
            List < TapDetail > tapDetailList = tap.getTapDetailList();
            for (OverseasWarehouseDetail overseasWarehouseDetail : overseasWarehouseDetailList) {
                for (TapDetail tapDetail : tapDetailList) {
                    if (overseasWarehouseDetail.getRowNumber().equals(tapDetail.getRowNumber())) {
                        if (StringUtils.isEmpty(tapDetail.getFrameNumber())) {
                            tapDetail.setFrameNumber(overseasWarehouseDetail.getFrameNumber());
                        }
                    }
                }
            }
            tapService.save(tap);
        }
    }

    /**
     * 更新状态
     *
     * @param overseasWarehouse
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(OverseasWarehouse overseasWarehouse) {
        super.updateStatus(overseasWarehouse);
    }

    /**
     * 删除数据
     *
     * @param overseasWarehouse
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(OverseasWarehouse overseasWarehouse) {
        super.delete(overseasWarehouse);
        OverseasWarehouseDetail overseasWarehouseDetail = new OverseasWarehouseDetail();
        overseasWarehouseDetail.setOverseasWarehouseId(overseasWarehouse);
        overseasWarehouseDetailDao.deleteByEntity(overseasWarehouseDetail);
    }

}